package org.glut.wechat.addresslist.controller;

import jakarta.annotation.Resource;
import org.glut.wechat.addresslist.common.Result;
import org.glut.wechat.addresslist.entity.User;
import org.glut.wechat.addresslist.service.impl.RelationServiceImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好友表 前端控制器
 *
 * @author harry
 * @since 2025-08-20 21:42
 */
@RestController
@RequestMapping("/relation")
// 添加跨域注解，允许来自前端的请求
@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:5174"})
public class RelationController {

    @Resource
    private RelationServiceImpl relationService;

    /**
     * 获取好友列表
     * @param userId 本人ID
     * @return 好友列表
     */
    @GetMapping("/friends")
    public Result<List<User>> getFriendList(@RequestParam("userId") Integer userId) {
        try {
            List<User> friends = relationService.getFriendsByUserId(userId);
            return Result.success(friends);
        } catch (Exception e) {
            return Result.fail("获取好友列表失败：" + e.getMessage());
        }
    }
    //好友详情信息
    @GetMapping("/friend/detail/{id}")
    public Result<User> getFriendDetail(@PathVariable Integer id) {
        try {
            User user = relationService.getFriendDetail(id);
            return Result.success(user);
        } catch (Exception e) {
            return Result.fail("获取好友详情失败：" + e.getMessage());
        }
    }
}
